﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing.Imaging;

namespace Meeting.Office2Image
{
    public class OfficeToImage
    {

        public static string errorString = "";

        /// <summary>
        ///   把office文档转换成iamge      
        /// <returns></returns>
        public static bool ConvertToImage(string inputPath, string outputPath, string imageName, out int imageCount)
        {
            imageCount = 0;
            try
            {
                if (inputPath == "") throw new Exception("没有找到要转换的文件");
                if (outputPath == "") throw new Exception("没有找到输出目录");
                string fileExt = Path.GetExtension(inputPath).ToLower();
                if (fileExt == ".doc" || fileExt == ".docx")
                {
                    Word2Image.ConvertWordToImage(inputPath, outputPath, "", 0, 0, ImageFormat.Jpeg, 120, out imageCount);
                }
                else if (fileExt == ".xls" || fileExt == ".xlsx")
                {
                    Excel2Image.ConvertExcelToImage(inputPath, outputPath, imageName, 0, 0, ImageFormat.Jpeg, out imageCount);
                }
                else if (fileExt == ".ppt" || fileExt == ".pptx")
                {
                    PPT2Image.ConvertPptToImage(inputPath, outputPath, "", 0, 0, ImageFormat.Jpeg, new System.Drawing.Size(800, 600), out imageCount);
                }
                else if (fileExt == ".pdf")
                {
                    PDF2Image.ConvertPDF2Image(inputPath, outputPath, imageName, 0, 0, ImageFormat.Jpeg, PDF2Image.Definition.Two, out imageCount);
                }
                else
                {
                    throw new Exception("不支持此格式的文件转换");
                }
                return true;
            }
            catch (Exception ex)
            {
                errorString = ex.Message;
                return false;
            }
        }

    }
}
